package queueDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2024-04-21
 * Time: 10:53
 */
public class MyLinkQueue {

    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    public int useSize;

    public boolean offer(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        useSize++;
        return true;
    }

    public int poll () {
        if (head == null) {
            return -1;
        }
        int val = head.val;
        if (head.next == null) {

            head = null;
            last = null;
            useSize--;
            return val;
        }
        head = head.next;
        head.prev = null;
        useSize--;
        return val;
    }

    public int peek() {
        if (head == null) {
            return -1;
        }
        return head.val;
    }

    public boolean empty() {
        return head == null;
    }

    public int size() {
        return useSize;
    }
}
